package com.example.smart_city_0829.activity.government;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.entity.Base;
import com.example.smart_city_0829.entity.gov.GovType;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.google.gson.reflect.TypeToken;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

public class GovernmentPubActivity extends BaseActivity {

    private LinearLayout ll1;
    private Spinner spinner;
    private EditText titleEt;
    private EditText contentEt;
    private EditText undertakeEt;
    private EditText phoneEt;
    private Button pubBt;

    private int appealCategoryId;
    private String appealCategoryName;

    private List<GovType.RowsBean> typeData;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_government_pub;
    }

    @Override
    protected void initView() {
        ll1 = (LinearLayout) findViewById(R.id.ll1);
        spinner = (Spinner) findViewById(R.id.spinner);
        titleEt = (EditText) findViewById(R.id.title_et);
        contentEt = (EditText) findViewById(R.id.content_et);
        undertakeEt = (EditText) findViewById(R.id.undertake_et);
        phoneEt = (EditText) findViewById(R.id.phone_et);
        pubBt = (Button) findViewById(R.id.pub_bt);
    }

    @Override
    protected void initData() {
        //拿一个诉求列表数据，如果不为空就说明是从其他诉求点进来的
        String gsonData =  getIntent().getStringExtra("typeData");
        if (gsonData != null){
            typeData = gson.fromJson(gsonData,new TypeToken<List<GovType.RowsBean>>(){}.getType());
        }
        if (typeData != null){
            appealCategoryId = typeData.get(typeData.size()-1).getId();
            ll1.setVisibility(View.VISIBLE);
            spinner.setAdapter(new ArrayAdapter<>(
                    this,
                    android.R.layout.simple_spinner_dropdown_item,
                    typeData.stream().map(GovType.RowsBean::getName).collect(Collectors.toList())
                )
            );
        }else {
            appealCategoryId = getIntent().getIntExtra("appealCategoryId",-1);
            appealCategoryName = getIntent().getStringExtra("appealCategoryName");
            spinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, Collections.singletonList(appealCategoryName)));
        }
    }

    @Override
    protected void initEvent() {
        pubBt.setOnClickListener(v -> {
            pubBt.setEnabled(false);

            final HashMap<String, Object> params = new HashMap<>();
            params.put("appealCategoryId",appealCategoryId);
            params.put("title",titleEt.getText().toString());
            params.put("content",contentEt.getText().toString());
            params.put("undertaker",undertakeEt.getText().toString());
            params.put("imgUrl","/profile/abc.png");

            RequestBody body = RequestBody.create(gson.toJson(params), MediaType.parse("application/json"));
            api.postGovAdv(body).enqueue(new NetCallBack<Base>() {
                @Override
                public void onResponse(Call<Base> call, Response<Base> response) {
                    pubBt.setEnabled(true);
                    if (response.body()!=null) {
                        showToastShort(response.body().getMsg());
                    }
                }
            });
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "创建诉求";
    }
}